#define LOG_TAG "Checksum"
#include <cstdio>

#include "aes.h"
#include "crc32.h"
#include "log/log_main.h"
#include <string.h>

#define PINCODE_START_ADD 0
#define PINCODE_DATA_SIZE 5

#define KEYCODE_START_ADD 5
#define KEYCODE_DATA_SIZE 260

#define KEYCODEHASH_START_ADD 265
#define KEYCODEHASH_DATA_SIZE 32

#define PINCODE_WRITE_FLAG_START_ADD                                           \
  (KEYCODEHASH_START_ADD + KEYCODEHASH_DATA_SIZE)
#define PINCODE_WRITE_FLAG_DATA_SIZE 2

#define KEYCODE_WRITE_FLAG_START_ADD                                           \
  (PINCODE_WRITE_FLAG_START_ADD + PINCODE_WRITE_FLAG_DATA_SIZE)
#define KEYCODE_WRITE_FLAG_DATA_SIZE 2

#define RESERVED_START_ADD                                                     \
  (KEYCODE_WRITE_FLAG_START_ADD +                                              \
   KEYCODE_WRITE_FLAG_DATA_SIZE) // reserved area add
#define RESERVED_DATA_SIZE                                                     \
  (KEY_STORAGE_AREA_SIZE - PINCODE_DATA_SIZE - KEYCODE_DATA_SIZE -             \
   KEYCODEHASH_DATA_SIZE - PINCODE_WRITE_FLAG_DATA_SIZE -                      \
   KEYCODE_WRITE_FLAG_DATA_SIZE - 4) // reserved area size

#define KEY_STORAGE_AREA_SIZE 2048 // size:2K
#define KEY_STORAGE_AREA_OFFSET 40 // 20*1024/512  Key offset=20K, size:2K
#define DEF_CRC32 0xAD35EF26

#define DEV_KEY_PATH "/dev/block/sda11"
enum { KEY_PINCODE_TYPE, KEY_KEYCODE_TYPE };

extern unsigned char ivec[16];
extern unsigned char key16[16];
extern unsigned char defKey[KEY_STORAGE_AREA_SIZE];

unsigned char defKey[KEY_STORAGE_AREA_SIZE] = {
    /* 5byte securityaccess pincode*/
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    /* 260byte checkmemory key*/
    0xb7, 0xe9, 0x74, 0x4b, 0x45, 0xfa, 0xa6, 0x20, 0xd3, 0x1c, 0x30, 0xe9,
    0x63, 0x86, 0xe9, 0xcd, 0x5f, 0xb9, 0x93, 0xde, 0xca, 0x45, 0xc9, 0xd6,
    0x08, 0x94, 0xf7, 0x7d, 0xb9, 0xee, 0xa9, 0xd0, 0x78, 0x45, 0x76, 0x94,
    0x80, 0x9d, 0xf7, 0x05, 0x24, 0xd7, 0x30, 0xe2, 0xc0, 0x0f, 0x04, 0x6e,
    0x60, 0x53, 0x23, 0xbd, 0x50, 0x03, 0xbf, 0x2c, 0xa9, 0xbb, 0xb4, 0x5c,
    0xc5, 0x11, 0x5a, 0x1d, 0xce, 0x25, 0x7d, 0x42, 0x03, 0x4f, 0x7e, 0x1c,
    0x7a, 0x3e, 0x1a, 0x68, 0xe8, 0x9a, 0x00, 0x10, 0x8d, 0x18, 0x28, 0xac,
    0x26, 0xbd, 0x71, 0xae, 0x4a, 0xc9, 0xb9, 0x23, 0x0b, 0x9b, 0xc1, 0x01,
    0x67, 0x46, 0xa9, 0x01, 0x5e, 0x70, 0xf1, 0xd9, 0xbd, 0x7f, 0x56, 0x4b,
    0x97, 0x61, 0x64, 0xff, 0xc1, 0xd9, 0x6e, 0x93, 0xab, 0x40, 0x66, 0xd5,
    0xcb, 0xf4, 0x02, 0xf5, 0xfc, 0x53, 0x11, 0x51, 0xa9, 0x80, 0x5c, 0x07,
    0x16, 0xab, 0xcb, 0x98, 0x25, 0xfe, 0x02, 0xf3, 0x89, 0x7e, 0x57, 0x91,
    0x7a, 0x64, 0xcc, 0x2c, 0x7a, 0x71, 0xe8, 0x83, 0x33, 0x59, 0x0a, 0xa9,
    0x59, 0x23, 0xcf, 0x4a, 0x6b, 0xe4, 0x24, 0x1a, 0xf7, 0x8c, 0xa9, 0x04,
    0x5d, 0x65, 0xb6, 0x74, 0x87, 0x19, 0x42, 0x49, 0xe3, 0x69, 0x03, 0xdd,
    0xa4, 0xc9, 0x75, 0xfe, 0xa7, 0x3c, 0x07, 0xc1, 0x91, 0x67, 0x54, 0x45,
    0xfe, 0x5f, 0xcf, 0x45, 0x72, 0xf8, 0xbd, 0x47, 0x95, 0xba, 0x81, 0xa7,
    0x54, 0x50, 0x55, 0x29, 0x92, 0x2f, 0x81, 0x82, 0x71, 0x9b, 0x43, 0x1c,
    0xeb, 0x27, 0x16, 0xca, 0x87, 0xe2, 0xba, 0x83, 0xa0, 0x1e, 0x85, 0xef,
    0x75, 0xe4, 0x63, 0x88, 0x2d, 0x0b, 0x53, 0x76, 0xb6, 0xb3, 0xd6, 0x68,
    0x19, 0xe2, 0x6c, 0x2b, 0x67, 0x4f, 0x0a, 0x9d, 0xde, 0xfe, 0x93, 0x42,
    0x43, 0xce, 0x87, 0xad, 0x00, 0x01, 0x00, 0x01,
    /* 32byte checkmemory key hash*/
    [KEYCODEHASH_START_ADD...(KEYCODEHASH_START_ADD + KEYCODEHASH_DATA_SIZE -
                              1)] = 0x00,
    /* 4byte flag |2 Bytes pincode write flag|2 Bytes key write flag|
       0x6666:already write other:no write*/
    [PINCODE_WRITE_FLAG_START_ADD...(PINCODE_WRITE_FLAG_START_ADD +
                                     PINCODE_WRITE_FLAG_DATA_SIZE - 1)] = 0x00,
    [KEYCODE_WRITE_FLAG_START_ADD...(KEYCODE_WRITE_FLAG_START_ADD +
                                     KEYCODE_WRITE_FLAG_DATA_SIZE - 1)] = 0x00,
    /* reserved */
    [RESERVED_START_ADD...(KEY_STORAGE_AREA_SIZE - 1)] = 0x00};

typedef union {
  unsigned int crc;
  struct {
    char str[4];
  } data;
} nCrc32Val;

static unsigned long getDecryptData(unsigned char *keyDecrypt, unsigned int size);

bool getCheckMemKeyHash(unsigned char *checksum) {
  unsigned char keyDecrypt[KEY_STORAGE_AREA_SIZE];
  unsigned int len, ret = 0;
  unsigned char *pKey = keyDecrypt;

  memset(keyDecrypt, 0, sizeof(keyDecrypt));

  ret = getDecryptData(pKey, KEY_STORAGE_AREA_SIZE);
  if (ret != 0) {
    pKey = defKey;
    ALOGE("[SWDL_SBL] : %s Fail:%d\n", __func__, ret);
  }

  if ((0x66 != pKey[KEYCODE_WRITE_FLAG_START_ADD]) &&
      (0x66 != pKey[KEYCODE_WRITE_FLAG_START_ADD + 1])) {
    ALOGE("[SWDL_SBL] : %s public key hasn't been written! pKey flag:%x %x\n",
          __func__, pKey[KEYCODE_WRITE_FLAG_START_ADD],
          pKey[KEYCODE_WRITE_FLAG_START_ADD + 1]);
    return false;
  }
  memcpy(checksum, &pKey[KEYCODEHASH_START_ADD], KEYCODEHASH_DATA_SIZE);
  return true;
}

static unsigned long getDecryptData(unsigned char *keyDecrypt, unsigned int size) {
  unsigned char keyEncrypt[KEY_STORAGE_AREA_SIZE];
  unsigned int crc;
  unsigned int len, i, ret = 0;
  AES_KEY aes_ks;
  nCrc32Val crc32Val;
  unsigned char ivecTmp[16];
  memset(keyEncrypt, 0, sizeof(keyEncrypt));
  FILE *file;
  if ((file = fopen(DEV_KEY_PATH, "rb")) != nullptr) {
    fseek(file, KEY_STORAGE_AREA_OFFSET * 512, SEEK_SET);
    fread(keyEncrypt, sizeof(keyEncrypt), 1, file);
    fclose(file);
  }

  memset(&aes_ks, 0, sizeof(aes_ks));
  memcpy(ivecTmp, ivec, 16);
  AES_set_decrypt_key(key16, 128, &aes_ks);
  len = CRYPTO_cbc128_decrypt(keyEncrypt, keyDecrypt, sizeof(keyEncrypt),
                              &aes_ks, ivecTmp, (block128_f)AES_decrypt);
  if (len != sizeof(keyEncrypt)) {
    ALOGE("%s: CRYPTO_cbc128_decrypt len %d != 2048", __func__, len);
  }
  memcpy(crc32Val.data.str, &keyDecrypt[size - 4], 4);
  crc = crc32(DEF_CRC32, keyDecrypt, size - 4);
  if (crc != crc32Val.crc) {
    ALOGE("%s: crc32 failed read %d cal %d", __func__, crc32Val.crc, crc);
  }

  return ret;
}